package com.cgc.venus.server.job.jobdetail;

import com.cgc.venus.server.core.utils.StringUtil;
import com.cgc.venus.server.job.Constants;
import com.cgc.venus.server.job.bean.AppManageBean;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 统计平台应用
 */
public class StatsAppJob extends StatsAppBaseJob {
    private static Logger logger = LoggerFactory.getLogger(StatsAppJob.class);
    public StatsAppJob() {
    }
    public void execute(JobExecutionContext context)  
        throws JobExecutionException {
        logger.info("统计平台应用job start...");
        List objs = new ArrayList<Object>();
        String sql="";
        String id=null;
        try {
            //查询统计值
            List<AppManageBean> qr=queryAllAppInfo();
            int app_count_all=qr.size();
            int app_count_running=qr.stream().filter(ts -> StatsAppBaseJob.APP_STATUS_RUNNING.equals(ts.getStatus())).collect(Collectors.toList()).size();
            int app_count_close=qr.stream().filter(ts -> StatsAppBaseJob.APP_STATUS_CLOSE.equals(ts.getStatus())).collect(Collectors.toList()).size();
            List<AppManageBean> qrEx=qr.stream().filter(ts -> StatsAppBaseJob.APP_STATUS_ERROR.equals(ts.getStatus())).collect(Collectors.toList());
            int app_count_ex=qrEx.size();
            int app_count_today=qr.stream().filter(ts -> ts.isToday()).collect(Collectors.toList()).size();
            //处理告警
            if(app_count_ex>0){
                //往告警表插入一条记录
                String appExInfo="";
                for(AppManageBean amb:qrEx){
                    if(appExInfo.equals("")){
                        appExInfo+=amb.getAppName();
                    }else{
                        appExInfo+=","+amb.getAppName();
                    }
                }
                String alertMsg="平台应用有"+app_count_ex+"个有异常,异常应用名称:("+appExInfo+")";
                handleEx(alertMsg,Constants.ALARM_TYPE_APP,Constants.ALARM_STATUS_UN,Constants.ALARM_LEVEL_HIGHEST,Constants.ALARM_NS_ALL);
            }

            //更新统计值表
            sql="select id from t_job_stats_app where date_format(create_time,'%Y-%m-%d')=date_format(NOW(),'%Y-%m-%d')";
            List<Map<String, Object>> l=getJdbcTemplate().queryForList(sql);
            if(l!=null&&l.size()>0){
                //db update
                id=l.get(0).get("id").toString();
                sql = "update t_job_stats_app  set app_count_all=?,app_count_running=?,app_count_close=?,app_count_ex=?,app_count_today=?,last_update_time=NOW() where id=?";
            }else{
                //db insert
                id = StringUtil.getUUID();
                sql = "INSERT INTO t_job_stats_app(app_count_all,app_count_running,app_count_close,app_count_ex,app_count_today,id,create_time,last_update_time) VALUES (?,?,?,?,?,?,NOW(),NOW())";
            }
            objs.add(app_count_all);
            objs.add(app_count_running);
            objs.add(app_count_close);
            objs.add(app_count_ex);
            objs.add(app_count_today);
            objs.add(id);
            getJdbcTemplate().update(sql, objs.toArray());
        }catch (Exception ex){
            logger.error(ex.getMessage(),ex);
            handleEx("统计平台应用任务异常,异常信息:"+getErrorInfoFromException(ex),Constants.ALARM_TYPE_JOB_ERROR,Constants.ALARM_STATUS_UN,Constants.ALARM_LEVEL_HIGHEST,Constants.ALARM_NS_ALL);
            throw new JobExecutionException(ex);
        }
    }  
}